#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/CI/lib -I/data/Software/mydan/CI/private/lib
use strict;
use warnings;
use Logs;
use FindBin qw( $RealBin );
use Util;

$| ++;

=head1 SYNOPSIS

    db => $mysql,
    logs => 日志对象

    uuid => version的uuid

#只有使用uuid来构建的时候才允许send

=cut

return sub
{
    my %param = @_;

    my ( $db, $logs, $uuid, $projectid, $version ) = @param{qw( db logs uuid projectid version )};

    print "=" x 75, "\n";
    $logs = Logs->new( 'code.build.send.torepo' ) unless $logs;

    my %env = Util::envinfo( 'envname' );
    my $envname = $env{envname};
    print "torepo: $envname\n";
    $logs->die( "get envname fail" ) unless $envname && $envname =~ /^[a-zA-Z0-9]+$/;

    print "torepo($envname)...\n";

    my $pkgpath = "$RealBin/../logs/pkgs/$uuid";
    #my $cmd = "RSYNC_PASSWORD=openc3reporepo rsync -avP '$pkgpath' $envname.repo.ci.open-c3.org:/data/CI/repo/${projectid}_$version";

    my $repopath = "$RealBin/../repo/$projectid";

    unless( -d $repopath )
    {
        $logs->die( "run build.send.torepo fail:$!" ) if system "mkdir '$repopath'";
    }

    my $user = `id -un`;chop $user;
    my $sudo = $user eq 'root' ? '' : 'sudo ';

    my $cmd = "${sudo}rm -rf '$repopath/$version' && ${sudo}mv '$pkgpath' '$repopath/$version'";
    $logs->die( "run build.send.torepo fail:$!" ) if system $cmd;
}
